package cn.xn.hrms.api.controller;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import com.github.pagehelper.PageInfo;

import cn.xn.hrms.api.dto.res.DepartmentDTO;
import cn.xn.hrms.api.dto.req.DepartmentReqDTO;
import cn.xn.hrms.domain.service.DepartmentService;
import cn.xn.hrms.domain.utils.ResultVo;
import io.swagger.annotations.ApiOperation;

/**
 * @author ysz
 */
@RestController
public class DepartmentController {

    @Resource
    DepartmentService departmentService;

    /**
     * 分页查询部门列表
     *
     * @param page
     * @param size
     * @param departmentName
     * @return
     */
    @GetMapping("/department/list")
    @ApiOperation("分页查询部门列表")
    public PageInfo<DepartmentDTO> list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                        @RequestParam(value = "size", defaultValue = "10") Integer size,
                                        @RequestParam(required = false, value = "departmentName") String departmentName) {
        return departmentService.search(departmentName, page, size);
    }

    /**
     * 部门列表
     *
     * @return
     */
    @GetMapping("/department/all")
    @ApiOperation("部门列表")
    public List<DepartmentDTO> all() {
        return departmentService.all();
    }

    /**
     * 新增部门
     *
     * @param department
     * @return
     */
    @PostMapping("/department")
    @ApiOperation("新增部门")
    @RequiresRoles("admin")
    public ResultVo insert(@RequestBody @Valid DepartmentReqDTO department) {
        departmentService.insert(department);
        return ResultVo.success();
    }

    /**
     * 修改部门
     *
     * @param department
     * @param id
     * @return
     */
    @PutMapping("/department/{id}")
    @ApiOperation("修改部门")
    @RequiresRoles("admin")
    public ResultVo update(@RequestBody DepartmentReqDTO department, @PathVariable Integer id) {
        departmentService.updateById(id, department);
        return ResultVo.success();
    }

    /**
     * 删除部门（支持批量）
     *
     * @param id
     * @return
     */
    @DeleteMapping("/department/delete")
    @ApiOperation("删除部门（支持批量）")
    @RequiresRoles("admin")
    public ResultVo deleteIdIn(@RequestBody Set<Integer> id) {
        departmentService.deleteByIdIn(id);
        return ResultVo.success();
    }
}
